package com.bestbuy.controller;

import java.security.Principal;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.bestbuy.pojo.Account;
import com.bestbuy.pojo.Cart;
import com.bestbuy.pojo.CartDetail;
import com.bestbuy.pojo.Catalog;
import com.bestbuy.pojo.Product;
import com.bestbuy.service.AccountService;
import com.bestbuy.service.CartService;
import com.bestbuy.service.CatalogService;
import com.bestbuy.service.ProductService;

@Controller
public class IndexController {

	private static String username = "";
	@Autowired
	private CatalogService catalogService;
	@Autowired
	private ProductService productService;

	@Autowired
	private AccountService accountService;


	@Autowired
	private CartService cartService;
	// mac dinh khi build project
	@RequestMapping(value = { "/index.do", "" })
	public String getIndex(Model model) {
		//List<Catalog> listCatalog = catalogService.getListCatalog();
		List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
		model.addAttribute("catalogs", listCatalog);
		//List<Product> listProduct = productService.Search("", "0", "1");
		List<Product> listProduct = productService.Search("", "0", "1", "0");
		model.addAttribute("products", listProduct);
		model.addAttribute("isIndex", "SlideShow");
		model.addAttribute("Page", "Index");
		return "Main";
	}

	// khi dang nhap thanh cong
	@RequestMapping(value = { "/index.do" }, method = RequestMethod.GET)
	public String UserIndex(ModelMap model, Principal principal,
			HttpSession session)
	{
		//List<Catalog> listCatalog = catalogService.getListCatalog();
		List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
		model.addAttribute("catalogs", listCatalog);
		//List<Product> listProduct = productService.Search("", "0", "1");
		List<Product> listProduct = productService.Search("", "0", "1", "0");
		model.addAttribute("products", listProduct);
		String name = "";
		Account account = new Account();
		if (principal != null)
		{
			if (principal.getName() != null) {
				name = principal.getName();
				session.setAttribute("username", name);
				username = name;
				account = accountService.GetAccount(name);
			}
			if (account != null) 
			{
				if (account.getAccountRole().getRole().equals("Admin"))
				{
					return "redirect:/admin.do";
				} else 
				{
					model.addAttribute("isIndex", "SlideShow");
					model.addAttribute("Page", "Index");
				}
			}
		}
		else // neu dang nhap bang facebook
		{
			name = (String)session.getAttribute("username");
			if(name != null){
				//session.setAttribute("username",name);
				account = accountService.GetAccount(name);
				username = name;
				if (account.getAccountRole().getRole().equals("Admin"))
				{
					return "redirect:/admin.do";
				} else 
				{ 
					model.addAttribute("isIndex", "SlideShow");
					model.addAttribute("Page", "Index");
				}
			}else{
				model.addAttribute("isIndex", "SlideShow");
				model.addAttribute("Page", "Index");
			}
		}
		return "Main";
	}

	// thi thoat ra
	@RequestMapping(value = { "/home.do" }, method = RequestMethod.GET)
	public String LogoutAccount(ModelMap model, HttpSession session)
	{
		// huy session di
		session.removeAttribute("username");
		Cart cart = cartService.getCart(username);
		List<CartDetail> cartDetail = cartService.getCartDetail(cart
				.getIdcart());
		if(cartDetail.size() != 0){
			for (CartDetail cartdetail : cartDetail) {
				cartService.removeProduct(cartdetail.getIdcartdetail().toString());			
			}
		}
		cart.setTotal(0.0f);
		cartService.updateCart(cart);
		username = "";
		//List<Catalog> listCatalog = catalogService.getListCatalog();
		List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
		model.addAttribute("catalogs", listCatalog);
		//List<Product> listProduct = productService.Search("", "0", "1");
		List<Product> listProduct = productService.Search("", "0", "1", "0");
		model.addAttribute("products", listProduct);
		model.addAttribute("isIndex", "SlideShow");
		model.addAttribute("Page", "Index");
		return "Main";
	}
}
